﻿using System;
using System.Linq;
using System.Windows;
using ComicQ.Domain;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for MemberEditorView.xaml
    /// </summary>
    public partial class MemberEditorView
    {
        private readonly Member _member;

        public MemberEditorView()
        {
            InitializeComponent();

            Title = "Add Member";
        }

        public MemberEditorView(Member member)
        {
            InitializeComponent();

            Title = "Edit Member";

            _member = member;

            textBoxId.Text = member.Id.ToString();
            textBoxUsername.Text = member.Username;
            textBoxPassword.Text = member.Password;
            textBoxName.Text = member.Name;
            textBoxAddress.Text = member.Address;
            textBoxPhone.Text = member.Phone;
            textBoxEmail.Text = member.Email;
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxName.Text.Equals("") || textBoxAddress.Text.Equals("") ||
                textBoxPhone.Text.Equals("") ||textBoxEmail.Text.Equals(""))
            {
                MessageBox.Show("Field tidak boleh kosong.");
            }
            else
            {
                try
                {
                    var phoneDigits = textBoxPhone.Text.ToCharArray();

                    if (phoneDigits.Any(cell => !char.IsDigit(cell)))
                    {
                        throw new Exception();
                    }

                    if (Title.Equals("Add Member"))
                    {
                        using (var session = NHibernateHelper.OpenSession())
                        {
                            using (var transaction = session.BeginTransaction())
                            {
                                var member = new Member
                                                 {
                                                     Username = textBoxUsername.Text,
                                                     Password = textBoxPassword.Text,
                                                     Name = textBoxName.Text,
                                                     Address = textBoxAddress.Text,
                                                     Phone = textBoxPhone.Text,
                                                     Email = textBoxEmail.Text,
                                                     JoinDate = DateTime.Now
                                                 };

                                session.Save(member);
                                transaction.Commit();
                                MessageBox.Show("Add sukses.");
                            }
                        }
                    }
                    else
                    {
                        using (var session = NHibernateHelper.OpenSession())
                        {
                            var member = session.Query<Member>()
                                .Where(m => m.Id == _member.Id)
                                .Select(m => m)
                                .Single();

                            member.Username = textBoxUsername.Text;
                            member.Password = textBoxPassword.Text;
                            member.Name = textBoxName.Text;
                            member.Address = textBoxAddress.Text;
                            member.Email = textBoxEmail.Text;
                            member.Phone = textBoxPhone.Text;

                            using (var transaction = session.BeginTransaction())
                            {
                                session.SaveOrUpdate(member);
                                transaction.Commit();
                                MessageBox.Show("Edit sukses.");
                            }
                        }
                    }

                    Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Field phone harus angka.");
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
